const formele = document.querySelector('form')
let zhanghaoele = document.querySelector('input[name="username"]')
let passwordele = document.querySelector('input[name="password"]')
let nameele = document.querySelector('input[name="nickname"]')
let pele = document.querySelector('.buzhengque')
const reg = /^[A-Z][a-z0-9]{7,}$/

function onLogin() {
    //绑定登录事件
    formele.addEventListener('submit', function (e) {
        e = e || window.event //事件对象
        e.preventDefault() //阻止默认action行为
        if (reg.test(zhanghaoele.value) && reg.test(passwordele.value)) {
            axios({
                method: 'post',
                url: 'http://api.yuguoxy.com/api/member/add',
                data: {
                    username: zhanghaoele.value,
                    password: passwordele.value,
                }
            }).then(response => {
                let data = response.data
                if (data.resultCode == 1) {
                    // 1. 保存用户信息到localStorage
                    let user = data.resultInfo
                    localStorage.setItem('USER', JSON.stringify(user))
                    // 2. 跳转主界面
                    alert('注册成功！跳转到主页')
                    location.href = '../pages/index.html'
                }
            })
        } else {
            pele.style.display = 'block'
        }
    })
}

onLogin()